package com.tianqu.tms.www.attn.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsAction;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.Attn;
import com.tianqu.tms.entity.Member;
import com.tianqu.tms.entity.User;
import com.tianqu.tms.www.attn.viewhelper.AttnViewhelper;

/**
 * @author zhangxueliang
 * 经办人添加 中间跳转action
 */
public class AttnCreationInputConfirmAction extends TmsAction{
	private Logger logger = Logger.getLogger(AttnCreationInputConfirmAction.class);
	@Override
	protected ActionForward doAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		
		AttnViewhelper viewhelp = (AttnViewhelper)uc.getAttribute(TmsConstants.VIEWHELP);
		Member mem = Member.getInstance();
		if(!StringUtil.isNullOrBlank(viewhelp.getMemberId())){
			viewhelp.setMemberId(mem.getMemNameByMemId(Integer.parseInt(viewhelp.getMemberId())));
		}else{
			viewhelp.setMemberId("");
		}
		
		//用户唯一性验证
		request.setAttribute(TmsConstants.VIEWHELP, viewhelp);
		User user = new User();
		if(user.findUserInfoByUserId(viewhelp.getLoginName(),0)==false)
		{
			setErrorsToRequest(request, TmsConstants.IC001 ,null);
			return mapping.getInputForward();				
		}
		
		Attn attn = Attn.getInstance();
		if(!StringUtil.isNullOrBlank(viewhelp.getAccessCardId())){
			int mflag = attn.findAttnByAccessCardId(viewhelp.getAccessCardId());
			if(mflag == 2){
				setErrorsToRequest(request, TmsConstants.IC003 ,null);
				return mapping.getInputForward();
			}
			int cardNoFlag = mem.findMemberByAccessCardNo(viewhelp.getAccessCardNo());
			int attnFlag = attn.findAttnByAccessCardNo(viewhelp.getAccessCardNo());
			if(cardNoFlag == 2 || attnFlag == 2){
				setErrorsToRequest(request, TmsConstants.IC004 ,null);
				return mapping.getInputForward();
			}
		}
		logger.debug(this.getClass().getName() + "#execute was completed.");
		return mapping.findForward(TmsConstants.SUCCESS);
	}

}
